﻿Option Explicit On
Option Strict On

Module clsCommon

    Public Function fncShowErr(ByVal strMessage As String) As DialogResult

        Try
            Return MessageBox.Show(strMessage, clsConst.gcstrApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error)

        Catch ex As Exception
            MessageBox.Show(ex.Message, clsConst.gcstrApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Function

    Public Function fncShowWarning(ByVal strMessage As String, Optional ByVal ctrlFocusTo As Control = Nothing) As DialogResult
        Try
            If ctrlFocusTo IsNot Nothing Then ctrlFocusTo.Focus()

            Return MessageBox.Show(strMessage, clsConst.gcstrApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning)

        Catch ex As Exception
            fncShowErr(ex.Message)
        End Try
    End Function

    Public Function fncShowConfirm(ByVal strMessage As String, Optional ByVal ctrlFocusTo As Control = Nothing) As DialogResult
        Try
            If ctrlFocusTo IsNot Nothing Then ctrlFocusTo.Focus()

            Return MessageBox.Show(strMessage, clsConst.gcstrApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        Catch ex As Exception
            fncShowErr(ex.Message)
        End Try
    End Function

    Public Function fncShowInfo(ByVal strMessage As String, Optional ByVal ctrlFocusTo As Control = Nothing) As DialogResult
        Try
            If ctrlFocusTo IsNot Nothing Then ctrlFocusTo.Focus()

            Return MessageBox.Show(strMessage, clsConst.gcstrApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            fncShowErr(ex.Message)
        End Try
    End Function

    Public Function fncIsBlank(ByVal str As String) As Boolean
        fncIsBlank = False

        Try
            'str = str.Trim()

            Return String.IsNullOrEmpty(str)

            Return True
        Catch ex As Exception
            fncShowErr(ex.Message)
        End Try
    End Function


    Public Function fncCnvString(ByVal objValue As Object) As String

        fncCnvString = ""

        Try
            If objValue Is Nothing Then Exit Function

            fncCnvString = CStr(objValue)
            fncCnvString = fncCnvString.Trim()

        Catch ex As Exception
            fncShowErr(ex.Message)
        End Try

    End Function


    Public Function fncCnvInt(ByVal objValue As Object) As Integer

        fncCnvInt = 0

        Try
            If objValue Is Nothing Then Exit Function

            If fncIsBlank(objValue.ToString()) Then Exit Function

            If IsNumeric(objValue) Then

                fncCnvInt = CInt(objValue)

            End If

            
        Catch ex As Exception
            fncShowErr(ex.Message)
        End Try

    End Function

    ''' <summary>
    ''' Set Run at start up
    ''' </summary>
    ''' <param name="ApplicationName"></param>
    ''' <param name="ApplicationPath"></param>
    ''' <remarks></remarks>
    Public Function fncRunAtStartup(ByVal ApplicationName As String, ByVal ApplicationPath As String) As Boolean
        fncRunAtStartup = False
        Try
            Dim CU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
            With CU
                .OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)

                .SetValue(ApplicationName, ApplicationPath)

            End With
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    ''' <summary>
    ''' Remove run at startup
    ''' </summary>
    ''' <param name="ApplicationName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function fncRemoveValue(ByVal ApplicationName As String) As Boolean
        fncRemoveValue = False
        Try
            Dim CU As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
            With CU
                .OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)

                .DeleteValue(ApplicationName, False)

            End With
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

End Module

Public Class clsItem

    Private mstrText As String
    Private mobjValue As Object

    Public ReadOnly Property Text As String
        Get
            Return mstrText
        End Get
    End Property

    Public ReadOnly Property Value As Object
        Get
            Return mobjValue
        End Get
    End Property

    Public Sub New(ByVal strText As String, ByVal objValue As Object)
        mstrText = strText
        mobjValue = objValue
    End Sub

    Public Overrides Function ToString() As String
        Return mstrText
    End Function

End Class
